package com.kiwi.cases.match.usermatch;

import com.beust.jcommander.Parameter;
import com.kiwi.cases.match.usermatch.GenerateParams;
import com.kiwi.constant.Constant;
import com.kiwi.constant.ConstantURL;
import com.kiwi.util.APIEncryptSet;
import com.kiwi.util.EncryptSet.Impl.SignatureServiceImpl;
import com.netease.shockwave.http.HttpHandler;
import com.netease.shockwave.testng.ShockwaveTestNG;
import com.sun.xml.internal.bind.v2.TODO;
import jxl.read.biff.BiffException;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.annotations.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.kiwi.util.MD5Util;

import static com.kiwi.constant.ConstantURL.GAEA_USER_MATCH_DIR;
import static com.kiwi.util.excel.JxlReader.jxlReader;

/**
 * 测试接口：密码登录接口 http://uc.s.kiwi.com/api/login
 *
 * @param
 * @param
 * @author hancongcong
 */
public class UserMatch extends ShockwaveTestNG
{
    private static ArrayList<String> sheetNames;
    private  Logger logger = Logger.getLogger(UserMatch.class);

    @BeforeClass
    public void BeforeClassTest()
    {
        logger.info("@test：用户随机匹配测试开始");

    }

    @Test(description = "用户随机匹配接口/user/match", dataProvider = "UserMatchData")
    public void testUserMatch(String param) throws Exception {
        System.out.println("@UserMatch---未加密参数为："+param);
        param = APIEncryptSet.enCode(param);
        System.out.println("@UserMatch---加密参数为:"+param);
        String[] paramsEncode = param.split("&");
        String data = paramsEncode[0].substring(5,paramsEncode[0].length());
        String stamp = paramsEncode[1].substring(6,paramsEncode[1].length());
        System.out.println("@UserMatch---data="+data);
        System.out.println("@UserMatch---stamp="+stamp);
        HashMap<String, String> httpParams = new HashMap<String, String>();
        httpParams.put("data",data);
        httpParams.put("stamp",stamp);
        HttpHandler handler = HttpHandler.getInstance();
        String enResult = handler.usingPostMethod(ConstantURL.GAEA_USER_MATCH,httpParams,null);
        logger.info("@UserMatch---请求结果"+enResult);
        //判断result返回100
        Assert.assertTrue(enResult.contains("100"));
        Thread.sleep(2000);
    }
    @Test(description = "用户随机匹配结果接口/user/match/result", dataProvider = "UserMatchResultData")
    public void testUserMatchResult(HashMap<String,String> paramAndExpect)throws Exception{
        String param = paramAndExpect.get("param");
        System.out.println("@UserMatchResult---未加密参数为："+param);
        param = APIEncryptSet.enCode(param);
        System.out.println("@UserMatchResult---加密的参数为:"+param);
        String[] paramsEncode = param.split("&");
        String data = paramsEncode[0].substring(5,paramsEncode[0].length());
        String stamp = paramsEncode[1].substring(6,paramsEncode[1].length());
        System.out.println("@UserMatchResult---data="+data);
        System.out.println("@UserMatchResult---stamp="+stamp);
        HashMap<String, String> httpParams = new HashMap<String, String>();
        httpParams.put("data",data);
        httpParams.put("stamp",stamp);
        HttpHandler handler = HttpHandler.getInstance();
        int times = 0;
        do{
            Thread.sleep(2000);
            String enResult = handler.usingPostMethod(ConstantURL.GAEA_USER_MATCH_RESULT,httpParams,null);
            logger.info("@UserMatchResult---请求结果:"+enResult);
            times ++;
        }while(times<6);

        String expect = paramAndExpect.get("expect");
        System.out.println("@UserMatchResult---测试期望为:"+expect);
        String[] expects = expect.split(",");
        System.out.println("@UserMatchResult---用户1:"+expects[0]);
        System.out.println("@UserMatchResult---用户2:"+expects[1]);
        //包含匹配的两个用户
        //Assert.assertTrue(enResult.contains(expects[0]));
        //Assert.assertTrue(enResult.contains(expects[1]));



    }

    @DataProvider(name = "UserMatchData",parallel = true)
    public static Iterator<Object[]> dataUserMatch(ITestContext context) throws IOException, BiffException {
        String xlsName = "test.xls";
        String sheetName=context.getCurrentXmlTest().getParameter("sheetName");
        System.out.println("@UserMatch---"+xlsName);
        System.out.println("@UserMatch---"+sheetName);
        return GenerateParams.getMatchParams(jxlReader( GAEA_USER_MATCH_DIR +xlsName,sheetName));
    }
    @DataProvider(name = "UserMatchResultData",parallel = true)
    public static Iterator<Object[]> dataUserMatchResult(ITestContext context) throws IOException, BiffException {
        String xlsName = "test.xls";
        String sheetName=context.getCurrentXmlTest().getParameter("sheetName");
        System.out.println("@UserMatchResult---:"+xlsName);
        System.out.println("@UserMatchResult---:"+sheetName);
        return GenerateParams.getMatchResultParams(jxlReader(GAEA_USER_MATCH_DIR +xlsName,sheetName));
    }

    @AfterClass
    public void AfterClassTest()
    {

        logger.info("用户随机匹配测试结束");
    }


}
